<template>
  <div>
    <a-form v-show="type === 'form'" :form="form" labelAlign="left" @submit="handleSubmit">
      <p class="title">社区服务中心意见</p>
      <a-form-item>
        <div class="a-flex a-flex-wrap">
          <span>经调查初审，该申请家庭</span>
          <div class="m-x-10">
            <a-select
              v-decorator="['BeviewResult', { rules: [{ required: true, message: '请选择审核结果!' }] }]"
              size="small"
              @change="BeviewResultChange"
              placeholder="未审核"
              allowClear
              :options="options"
              style="width: 130px"
            >
            </a-select>
          </div>
          <span>我市公共租赁住房保障条件，公示无异议。</span>
        </div>
      </a-form-item>
      <!-- <a-form-item label="备注" :labelCol="{ span: 2 }" :wrapperCol="{ span: 14 }">
        <a-textarea v-decorator="['remark']" :auto-size="{ minRows: 2, maxRows: 4 }" />
      </a-form-item> -->
      <a-row :gutter="24">
        <a-col :span="8">
          <a-form-item label="经办人" :labelCol="{ span: 6 }" :wrapperCol="{ span: 18 }">
            <a-input v-decorator="['BeviewAgentx']" disabled></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="审核时间" :labelCol="{ span: 6 }" :wrapperCol="{ span: 18 }">
            <a-input v-decorator="['BeviewDatex']" disabled></a-input>
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item style="text-align: right" :wrapperCol="{ span: 24 }">
        <y-button type="primary" :disabled="isButtonDisabled" shape="round" html-type="submit">确定提交</y-button>
        <y-button type="default" plain shape="round" @click="handleReset">取消提交</y-button>
      </a-form-item>
    </a-form>
    <a-space v-show="type === 'info'" direction="vertical">
      <p class="title">社区服务中心意见</p>
      <p>
        经调查初审，该申请家庭<span
          class="result"
          :class="data.BeviewResult === '符合' ? 'text-success' : 'text-error'"
          >{{ data.BeviewResult }}</span
        >我市公共租赁住房保障条件，公示无异议。
      </p>
      <!-- <p>备注：备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注备注。</p> -->
      <p>经办人：{{ data.BeviewAgentx }}&ensp;&ensp;&ensp;审核日期：{{ data.BeviewDatex | moment('YYYY-MM-DD') }}</p>
    </a-space>
  </div>
</template>

<script>
import moment from 'moment'
import { postProposerExamine, judgeIdCards } from '@/api/common'

export default {
  props: {
    type: {
      type: String,
      default: 'form',
    },
    reportData: {
      type: Object,
    },
  },
  inject: ['proposerData', 'handleClose'],
  computed: {
    data() {
      return this.proposerData()
    },
  },
  watch: {
    data: {
      deep: true,
      immediate: true,
      handler(n, o) {
        if (!this.$isEmpty(n) && this.type === 'form') {
          this.isBeviewResult = n.BeviewResult
          const { BeviewResult } = n
          if (!this.$isEmpty(BeviewResult) && BeviewResult !== '未审核') {
            this.form.setFieldsValue({
              BeviewResult,
            })
          }
        }
      },
    },
  },
  data() {
    return {
      form: this.$form.createForm(this, { name: 'cityOpinion' }),
      isBeviewResult: '',
      isButtonDisabled: false,
      options: [
        {
          value: '未审核',
          label: '未审核',
        },
        {
          value: '符合',
          label: '符合',
        },
        {
          value: '不符合',
          label: '不符合',
        },
      ],
    }
  },
  mounted() {
    if (this.type === 'form') {
      this.setDefault()
    }
  },
  methods: {
    // 设置默认值
    setDefault() {
      this.$nextTick(() => {
        this.form.setFieldsValue({
          BeviewAgentx: this.$store.getters.nickname,
          BeviewDatex: moment(new Date()).format('YYYY-MM-DD'),
        })
      })
    },
    BeviewResultChange() {
      if (this.reportData.BeviewResult == '不符合') {
        judgeIdCards({
          applyno: this.reportData.ApplyNO,
          id: this.reportData.ID,
        }).then((res) => {
          const h = this.$createElement
          this.$info({
            title: '提示!',
            content: h('div', {}, [h('p', res[0].msg)]),
            onOk() {},
          })
          if (res[0].state == '1') {
            this.isButtonDisabled = true
          }
        })
      }
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$loading.show({
            tip: '提交中，请稍后...',
          })
          const { BeviewAgentx, BeviewDatex, BeviewResult } = values
          postProposerExamine({
            State: 'CommunityAudit',
            ProposerModules: {
              ID: this.$route.params.id,
              BeviewAgentx,
              BeviewDatex,
              BeviewResult,
              LastAlterDate: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
              LastAlterUserID: this.$store.getters.userid,
            },
          })
            .then((res) => {
              this.$emit('submit')
            })
            .finally(() => {
              this.$loading.hide()
            })
        }
      })
    },
    handleReset() {
      this.form.resetFields(['BeviewResult'])
      this.handleClose()
    },
  },
}
</script>
<style scoped>
.title {
  color: rgba(0, 0, 0, 0.85);
  font-weight: 700;
  font-size: 16px;
  line-height: 1.5;
}
.result {
  font-size: large;
  padding: 0 10px;
  font-weight: bold;
}
</style>
